package com.rongj.generation.fx;

import com.rongj.generation.core.GenerationHandle;
import com.rongj.generation.core.util.Propertys;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.text.Text;
import org.apache.commons.lang.StringUtils;

public class MainController {

    @FXML
    private Text tips;
    @FXML
    private TextField tableName;
    @FXML
    private TextField entityName;

    @FXML
    private TextField entityPackage;

    @FXML
    private ToggleGroup  addd = new ToggleGroup();

    @FXML public void handleSubmitButtonAction(ActionEvent event) {
        if(StringUtils.isBlank(tableName.getText())) {
            tips.setText("请输入表名");
            return;
        }
        if(StringUtils.isBlank(entityName.getText())) {
            tips.setText("请输入实体类名");
            return;
        }
        if(StringUtils.isBlank(entityPackage.getText())) {
            tips.setText("请输入包名");
            return;
        }
        if(addd.getSelectedToggle() == null){
            tips.setText("请选择");
            return;
        }
        boolean hibernate = true;
        if(!addd.getSelectedToggle().getUserData().equals("hibernate"))
            hibernate = false;
        try {
            GenerationHandle handle = new GenerationHandle();
            handle.execute(tableName.getText(), Propertys.getKey("base-package"), entityName.getText(), entityPackage.getText(), hibernate);
            tips.setText("创建成功");
        }catch (Exception e){
            e.printStackTrace();
            tips.setText("创建失败："+e.getMessage());
        }
    }

    @FXML protected void closeWindowAction(ActionEvent event){
        ((Node) (event.getSource())).getScene().getWindow().hide();
    }
}
